package com.ruoyi.project.zxbd.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.zxbd.service.impl.BdPartnerServiceImpl;
import com.ruoyi.project.zxcrm.service.impl.CrmLeadServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 客户Controller
 *
 * @author mn
 * @date 2022-12-30
 */
@RestController
@RequestMapping("/bd/partner")
public class BdPartnerController extends BaseController {
    @Autowired
    private BdPartnerServiceImpl bdPartnerService;

    @Autowired
    private CrmLeadServiceImpl crmLeadService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询客户列表
     */

    @PreAuthorize("@ss.hasPermi('bd:partner:list')")
    @GetMapping("/list")
    public TableDataInfo list(){
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = bdPartnerService.selectBdPartnerListPd(pd);
        return getDataTable(list);
    }
    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('bd:partner:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageData pd) {
        List<PageData> list = bdPartnerService.selectBdPartnerListPd(pd);
        ExcelUtil<PageData> util = new ExcelUtil<PageData>(PageData.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('bd:partner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        PageData pd = bdPartnerService.selectBdPartnerById(id);
//        String user_id = pageData.get("user_id").toString();
//        user_id=bdPartnerMapper.selectDictLabelByUserIdPd(user_id);
//        pageData.put("user_id",user_id);
        return AjaxResult.success(pd);
//        return AjaxResult.success(BdPartnerService.selectBdPartnerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('bd:partner:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd) {
        //pd.put("user_id",SecurityUtils.getUserId());
//        pd.put("update_by", SecurityUtils.getUserId()+ "");
        return toAjax(bdPartnerService.insertBdPartnerPd(pd));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('bd:partner:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd) {
        return toAjax(bdPartnerService.updateBdPartnerPd(pd));
    }

    /**
     * 获取用户归属
     */
    @ApiOperation("取走客户")
    @PreAuthorize("@ss.hasPermi('bd:partner:edit')")
    @GetMapping(value = "/take/{id}")
    public AjaxResult TakeCustomer(@PathVariable("id") Long id) {
        PageData pd = new PageData();
        pd.put("id",id);
        pd = bdPartnerService.selectBdPartnerById(id);
        if (pd == null) {
            return AjaxResult.error("未找到该联系人信息");
        }

        if (pd.get("user_id").equals(this.getUserId())) {
        } else {
            return AjaxResult.error("该联系人属于：" + pd.get("user_id"));
        }
        pd.put("user_id",this.getUserId());
        bdPartnerService.updateBdPartnerPd(pd);
        crmLeadService.updateCrmLead(pd);
        return AjaxResult.success("取出成功");
    }
    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('bd:partner:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bdPartnerService.deleteBdPartnerByIds(ids));
//        return toAjax(BdPartnerService.deleteBdPartnerByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('bd:partner:privilege')")
    @Log(title = "权限归属客户", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/{s}")
    public AjaxResult privilegeSalas(@PathVariable Long[] ids, @PathVariable Long id) {
        return toAjax(bdPartnerService.updateSales(ids, id));
    }

}
